﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XeBus.BusinessLogic;
using XeBus.Controller;
using XeBus.BusinessObject;

namespace XeBus.Controller
{
    public partial class frmConDuong_LoTrinh : Form
    {
        private ConDuong_LoTrinh clObj = new ConDuong_LoTrinh();
        private ConDuong_LoTrinhBUS clBUS = new ConDuong_LoTrinhBUS();
        private DataTable dt = new DataTable();
        private int y;

        public frmConDuong_LoTrinh()
        {
            InitializeComponent();
            Load_ConDuong_LoTrinh();
        }

        private void Load_ConDuong_LoTrinh()
        {
            dgvCD_LT.AutoGenerateColumns=false;
            dgvCD_LT.DataSource = clBUS.ConDuong_LoTrinh();
        }

        private void btThemMoi_Click(object sender, EventArgs e)
        {
            frmThemSuaCD_LT cl = new frmThemSuaCD_LT();
            cl.lblID.Text = "";
            cl.ShowDialog();
            Load_ConDuong_LoTrinh();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            frmThemSuaCD_LT cl = new frmThemSuaCD_LT();
            cl.label3.Text = "Sửa: ID";
            cl.lblID.Text = dt.Rows[0]["ID"].ToString();
            cl.cbbMaLoTrinh.Text = dt.Rows[0]["Ma_Lo_Trinh"].ToString();
            cl.cbbMaConDuong.Text = dt.Rows[0]["Ma_Con_Duong"].ToString();
            cl.txtSoThuTu.Text = dt.Rows[0]["So_Thu_Tu"].ToString();
            cl.ShowDialog();
            Load_ConDuong_LoTrinh();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có chắc chắn muốn xóa không ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    y = dgvCD_LT.CurrentCell.RowIndex;
                    string ID = dgvCD_LT.Rows[y].Cells[0].Value.ToString();
                    clBUS.XOA(ID);
                    MessageBox.Show("Xóa dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    MessageBox.Show("Xóa dữ liệu thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                Load_ConDuong_LoTrinh();
            }
        }

        private void btDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Hien_Thi(int y)
        {
            string id = dgvCD_LT.Rows[y].Cells[0].Value.ToString();
            dt = clBUS.ConDuong_LoTrinhTheoID(id);

            lblID.Text = dt.Rows[0]["ID"].ToString();
            lblMaLoTrinh.Text = dt.Rows[0]["Ma_Lo_Trinh"].ToString();
            lblMaConDuong.Text = dt.Rows[0]["Ma_Con_Duong"].ToString();
            lblSoThuTu.Text = dt.Rows[0]["So_Thu_Tu"].ToString();
            lblTenConDuong.Text = dt.Rows[0]["Ten_Duong"].ToString();
            lblTenLoTrinh.Text = dt.Rows[0]["Ten_Lo_Trinh"].ToString();
        }

        private void dgvCD_LT_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            y = e.RowIndex;
            Hien_Thi(y);
        }
    }
}
